package dhm.com.dhmshop.framework.module.home.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.home.ListHotGoodsAdapter;
import dhm.com.dhmshop.adapter.mine.ShopAdapter;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetMyShopinfo;
import dhm.com.dhmshop.framework.base.BaseActivity;
import dhm.com.dhmshop.framework.module.commodity.activity.CommodityDetailsActivity;
import dhm.com.dhmshop.framework.module.home.entity.ListHotGoodsEntity;
import dhm.com.dhmshop.framework.module.home.model.ListHotGoodsModel;
import dhm.com.dhmshop.utils.SpUtils;

public class ListHotGoodsActivity extends BaseActivity<ListHotGoodsModel> implements LoginContract.IView {

    @BindView(R.id.xiao)
    TextView xiao;
    @BindView(R.id.price)
    CheckBox price;
    @BindView(R.id.priceup)
    ImageView priceup;
    @BindView(R.id.pricedown)
    ImageView pricedown;
    @BindView(R.id.renqi)
    TextView renqi;
    @BindView(R.id.pai)
    LinearLayout pai;
    @BindView(R.id.image)
    LinearLayout image;
    private int page = 0;
    private ListHotGoodsAdapter listHotGoodsAdapter;
    private List<ListHotGoodsEntity.DataBean> listHotGoodstitles;
    private String name;
    private int cate_id = 0;
    private PressenterImpl pressenter;
    private String uid;
    private int offset;
    private XRecyclerView listHotGoodsRecycler;
    private List<GetMyShopinfo.DataBean> allSearch;
    private ShopAdapter shopAdapter;
    private ImageView hotGoodsFan;
    private TextView title;


    @SuppressLint("NewApi")
    @Override
    protected void initView() {
        getWindow().setStatusBarColor(Color.WHITE);
        ButterKnife.bind(this);
        if (getIntent() != null) {
            name = getIntent().getStringExtra("goods_name");
            cate_id = getIntent().getIntExtra("cate_id", 0);
        }
        uid = SpUtils.getString(ListHotGoodsActivity.this, "uid");
        listHotGoodsRecycler = findViewById(R.id.list_hot_goods);
        hotGoodsFan = findViewById(R.id.hotgoods_fan);
        title = findViewById(R.id.title);

        pressenter = new PressenterImpl();
        pressenter.attachView(this);

        GridLayoutManager manager = new GridLayoutManager(ListHotGoodsActivity.this, 2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        offset = 1;
        listHotGoodsRecycler.setLayoutManager(manager);
        listHotGoodsRecycler.setLoadingMoreEnabled(true);
        listHotGoodsRecycler.setPullRefreshEnabled(true);

        hotGoodsFan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if (cate_id != 0) {
            offset = 1;
            title.setText(name);
            listHotGoodsRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    offset = 1;
                    search();
                }
                @Override
                public void onLoadMore() {
                    search();
                }
            });
        } else {
            listHotGoodsRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    offset = 1;
                    remen();
                }

                @Override
                public void onLoadMore() {
                    remen();
                }
            });
        }
    }

    private void search() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("page", offset + "");
        map.put("cate_id", cate_id + "");
        map.put("uid", uid);
        pressenter.sendMessage(ListHotGoodsActivity.this, Constant.CateSearch, map, GetMyShopinfo.class);
    }


    @Override
    protected void initData() {
        if (cate_id != 0) {
            pai.setVisibility(View.VISIBLE);
            search();
        } else {
            pai.setVisibility(View.GONE);
            remen();
        }

    }

    private void remen() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("page", offset + "");
        pressenter.sendMessage(ListHotGoodsActivity.this, Constant.ListHotGoods, map, ListHotGoodsEntity.class);
    }

    @Override
    protected void initListener() {
    }

    @Override
    protected int intiLayoutId() {
        return R.layout.activity_list_hot_goods;
    }

    @Override
    protected ListHotGoodsModel initModel() {
        return null;
    }


    @Override
    public void requesta(Object data) {
        if (data instanceof GetMyShopinfo) {
            GetMyShopinfo goodSearch = (GetMyShopinfo) data;
            if (goodSearch.getCode() == 1) {
                image.setVisibility(View.GONE);
                listHotGoodsRecycler.setVisibility(View.VISIBLE);
                if (offset == 1) {
                    allSearch = goodSearch.getData();
                    shopAdapter = new ShopAdapter(this);
                    shopAdapter.setaddClick(new ShopAdapter.OnIntentClick() {
                        @Override
                        public void item(int posttion) {
                            Intent intent = new Intent(ListHotGoodsActivity.this, CommodityDetailsActivity.class);
                            intent.putExtra("gid", allSearch.get(posttion).getGoods_id());
                            intent.putExtra("shop_id", allSearch.get(posttion).getShop_id());
                            startActivity(intent);
                        }
                    });
                    listHotGoodsRecycler.setAdapter(shopAdapter);
                } else {
                    allSearch.addAll(goodSearch.getData());
                }
                if (allSearch.size() > 0) {
                    listHotGoodsRecycler.setVisibility(View.VISIBLE);
                }
                shopAdapter.setShopList(allSearch);
                offset++;
            } else {
                if (offset==1){
                    image.setVisibility(View.VISIBLE);
                    listHotGoodsRecycler.setVisibility(View.GONE);
                }else {
                    Toast.makeText(this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                }
            }
            listHotGoodsRecycler.loadMoreComplete();
            listHotGoodsRecycler.refreshComplete();
        } else if (data instanceof ListHotGoodsEntity) {
            ListHotGoodsEntity listHotGoodsEntity = (ListHotGoodsEntity) data;
            if (listHotGoodsEntity.getCode() == 1) {
                image.setVisibility(View.GONE);
                listHotGoodsRecycler.setVisibility(View.VISIBLE);
                if (offset == 1) {
                    listHotGoodstitles = listHotGoodsEntity.getData();
                    listHotGoodsAdapter = new ListHotGoodsAdapter(ListHotGoodsActivity.this);
                    listHotGoodsRecycler.setAdapter(listHotGoodsAdapter);
                    listHotGoodsAdapter.setaddClick(new ListHotGoodsAdapter.OnShopClick() {
                        @Override
                        public void item(int posttion) {

                            Intent intent = new Intent(ListHotGoodsActivity.this, CommodityDetailsActivity.class);
                            intent.putExtra("shop_id", listHotGoodstitles.get(posttion).getGoods_id());
                            intent.putExtra("gid", listHotGoodstitles.get(posttion).getGoods_id());
                            startActivity(intent);
                        }
                    });
                } else {
                    listHotGoodstitles.addAll(listHotGoodsEntity.getData());
                }
                listHotGoodsAdapter.setShopList(listHotGoodstitles);
                offset++;
            } else {
                if (offset==1){
                    image.setVisibility(View.VISIBLE);
                    listHotGoodsRecycler.setVisibility(View.GONE);
                }else {
                    Toast.makeText(this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                }
            }
            listHotGoodsRecycler.refreshComplete();
            listHotGoodsRecycler.loadMoreComplete();
        }
    }

    @Override
    public void fail(String error) {

    }

    @OnClick({R.id.xiao, R.id.lin_price, R.id.renqi})
    public void onViewClicked(View view) {
        Map<String, String> map;
        switch (view.getId()) {
            case R.id.xiao:
                offset=1;
                map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", offset + "");
                map.put("cate_id", cate_id + "");
                map.put("uid", uid);
                map.put("sale_num", "1");
                pressenter.sendMessage(ListHotGoodsActivity.this, Constant.CateSearch, map, GetMyShopinfo.class);
                xiao.setTextColor(getResources().getColor(R.color.main));
                renqi.setTextColor(getResources().getColor(R.color.mainblack));
                price.setChecked(false);
                priceup.setImageResource(R.mipmap.priceupn);
                pricedown.setImageResource(R.mipmap.pricedownn);
                price.setTextColor(getResources().getColor(R.color.black));
                break;
            case R.id.lin_price:
                offset=1;
                price.setChecked(!price.isChecked());
                renqi.setTextColor(getResources().getColor(R.color.mainblack));
                xiao.setTextColor(getResources().getColor(R.color.mainblack));
                if (price.isChecked()){
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("page", offset + "");
                    map.put("cate_id", cate_id + "");
                    map.put("uid", uid);
                    map.put("price", "1");
                    pressenter.sendMessage(ListHotGoodsActivity.this, Constant.CateSearch, map, GetMyShopinfo.class);
                    priceup.setImageResource(R.mipmap.priceups);
                    pricedown.setImageResource(R.mipmap.pricedownn);
                }else {
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("page", offset + "");
                    map.put("cate_id", cate_id + "");
                    map.put("uid", uid);
                    map.put("price", "2");
                    pressenter.sendMessage(ListHotGoodsActivity.this, Constant.CateSearch, map, GetMyShopinfo.class);
                    priceup.setImageResource(R.mipmap.priceupn);
                    pricedown.setImageResource(R.mipmap.pricedowns);
                }
                price.setTextColor(getResources().getColor(R.color.main));
                break;
            case R.id.renqi:
                offset=1;
                map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", offset + "");
                map.put("cate_id", cate_id + "");
                map.put("uid", uid);
                map.put("renqi", "1");
                pressenter.sendMessage(ListHotGoodsActivity.this, Constant.CateSearch, map, GetMyShopinfo.class);
                xiao.setTextColor(getResources().getColor(R.color.mainblack));
                renqi.setTextColor(getResources().getColor(R.color.main));
                price.setChecked(false);
                priceup.setImageResource(R.mipmap.priceupn);
                pricedown.setImageResource(R.mipmap.pricedownn);
                price.setTextColor(getResources().getColor(R.color.black));
                break;
                default:
        }
    }
}
